# -*- coding: utf-8 -*-

class CMS < Control
  
  def initialize()
    super("sites")
  end
  
  def add_page_values(values)
    
    site = Sites.new()
    values = site.page_default_settings(values)
    values["header_option"] = ""
    
    menu = Categorys.new()
    values["menu"] = menu.get_menu("index.rb")
    
    if menu.get_page_num() != 0 then
      values["titlebar"] = "#{menu.get_page_name()} - " + values["title"]
    else
      #values["titlebar"] = values["title"]
    end
    
    item = Items.new()
    values["contents"] = item.get_items()
    #values["sidebar"] = item.get_sidebar()
    
    return values
    
  end
  
  def make_page_contents(mode, id)
    return ""
  end
  
  def output_html_files()
    
    html_open()
    output_dir = "../html/"
    
    html = ""
    values = {}
    
    site = Sites.new()
    
    css_file_name = site.get_css_file_name()
    
    src = open(css_file_name)
    dst = open(output_dir + css_file_name, "w")
    dst.write(src.read.gsub("../html/", ""))
    src.close
    dst.close
    
    
    #open(css_file_name) {|source|
    #  open(output_dir + css_file_name, "w") {|dest|
    #    dest.write(source.read)
    #  }
    #}
    
    menu = Categorys.new()
    pages = menu.get_data_for_html_files()
    
    pages.each {|row|
      
      if row["filename"] != "" then
        
        f = open(output_dir + row["filename"],'w')
        
        $_GET["c"] = row["id"]
        #html += "<div>#{row["filename"]}</div>"
        
        values = self.add_page_values(values)
        values["menu"] = menu.get_menu_for_html()
        
        #print values["titlebar"] + "<br>"
        
        html = @vw.load(values, $templete_dir + "page_main.html")
        pages.each {|row|
          html.gsub!("\"index.rb?c=#{row["id"]}\"", "\"#{row["filename"]}\"")
        }
        
        html.gsub!("../html/", "")
        
        f.print html
        f.close
        
      end
      
    }
    
    html = <<EOF
<div class='title'>#{$msg_cms_file_output}</div>
#{$msg_cms_file_output_complete}
<p><a href="#{output_dir}" target="_blank">#{$msg_cms_file_output_result}</a></p>
EOF

    
    values["contents"] = html
    values["menu"] = menu.get_menu("items.rb")
    values["titlebar"] = $msg_cms_file_output + " - " + values["title"]
    
    print @vw.load(values, $templete_dir + "page_main.html")
    
    terminate()
    html_close()
    
  end
  
end
